# -*- coding: utf-8 -*-
"""
boosting
Created on Sat Apr 28 17:08:20 2018

@author: Allen
"""
import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets

X, y = datasets.make_moons( n_samples = 500, noise = 0.3, random_state = 666 )

plt.scatter( X[y==0,0], X[y==0,1] )
plt.scatter( X[y==1,0], X[y==1,1] )
plt.show()

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, random_state = 666 )


### adaboosting
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier

ada_clf = AdaBoostClassifier( DecisionTreeClassifier( max_depth = 2 ), n_estimators = 500 )
ada_clf.fit( X_train, y_train )
print( ada_clf.score( X_test, y_test ) ) # 0.84

### gradient boosting
from sklearn.ensemble import GradientBoostingClassifier
gb_clf = GradientBoostingClassifier( max_depth = 2, n_estimators = 30 )
gb_clf.fit( X_train, y_train )
gb_clf.score( X_test, y_test )


### boosting 解决回归问题
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import GradientBoostingRegressor